);
/**
- * Perform pre-connection load ratio adjustment.
- * @param int[] &$weightByServer Map of (server index => integer weight)
- * @param string|bool $group The selected query group. Default: false
- * @param string|bool $domain Default: false
+ * Perform load ratio adjustment before deciding which server to use
+ *
+ * @param int[] &$weightByServer Map of (server index => float weight)
+ * @param string|bool $domain
*/
- public function scaleLoads( array &$weightByServer, $group = false, $domain = false );
+ public function scaleLoads( array &$weightByServer, $domain );
/**
* Get an estimate of replication lag (in seconds) for each server
*
* @param integer[] $serverIndexes
* @param string $domain
- *
* @return array Map of (server index => float|int|bool)
*/
public function getLagTimes( array $serverIndexes, $domain );
-
- /**
- * Clear any process and persistent cache of lag times
- * @since 1.27
- */
- public function clearCaches();
}
/** @var float Moving average ratio (e.g. 0.1 for 10% weight to new weight) */
private $movingAveRatio;
- const VERSION = 1;
+ const VERSION = 1; // cache key version
public function __construct(
ILoadBalancer $lb, BagOStuff $srvCache, BagOStuff $cache, array $options = []
$this->replLogger = $logger;
}
- public function scaleLoads( array &$weightByServer, $group = false, $domain = false ) {
+ public function scaleLoads( array &$weightByServer, $domain ) {
$serverIndexes = array_keys( $weightByServer );
$states = $this->getServerStates( $serverIndexes, $domain );
$coefficientsByServer = $states['weightScales'];
foreach ( $weightByServer as $i => $weight ) {
- $weightByServer[$i] = $weight * $coefficientsByServer[$i];
+ if ( isset( $coefficientsByServer[$i] ) ) {
+ $weightByServer[$i] = $weight * $coefficientsByServer[$i];
+ } else { // server recently added to config?
+ $host = $this->parent->getServerName( $i );
+ $this->replLogger->error( __METHOD__ . ": host $host not in cache" );
+ }
}
}
}
protected function getServerStates( array $serverIndexes, $domain ) {
- if ( count( $serverIndexes ) == 1 && reset( $serverIndexes ) == 0 ) {
+ $writerIndex = $this->parent->getWriterIndex();
+ if ( count( $serverIndexes ) == 1 && reset( $serverIndexes ) == $writerIndex ) {
# Single server only, just return zero without caching
return [
- 'lagTimes' => [ $this->parent->getWriterIndex() => 0 ],
- 'weightScales' => [ $this->parent->getWriterIndex() => 1 ]
+ 'lagTimes' => [ $writerIndex => 0 ],
+ 'weightScales' => [ $writerIndex => 1.0 ]
];
}
- $key = $this->getCacheKey();
+ $key = $this->getCacheKey( $serverIndexes );
# Randomize TTLs to reduce stampedes (4.0 - 5.0 sec)
$ttl = mt_rand( 4e6, 5e6 ) / 1e6;
# Keep keys around longer as fallbacks
return $conn ? 1.0 : 0.0;
}
- public function clearCaches() {
- $key = $this->getCacheKey();
- $this->srvCache->delete( $key );
- $this->mainCache->delete( $key );
- }
-
- private function getCacheKey() {
+ private function getCacheKey( array $serverIndexes ) {
+ sort( $serverIndexes );
// Lag is per-server, not per-DB, so key on the master DB name
return $this->srvCache->makeGlobalKey(
'lag-times',
self::VERSION,
- $this->parent->getServerName( $this->parent->getWriterIndex() )
+ $this->parent->getServerName( $this->parent->getWriterIndex() ),
+ implode( '-', $serverIndexes )
);
}
}